package adminService

import (
    adminModel "gin_http/internal/model/admin"
    adminRepository "gin_http/internal/repository/admin"
    "gin_http/utils/common"
    commonError "gin_http/utils/common/error"
)

type AuthService struct {
	UserRepo *adminRepository.UserRepository
}

func NewAuthService(adminRepo *adminRepository.UserRepository) *AuthService {
	return &AuthService{
		UserRepo: adminRepo,
	}
}

func (s *AuthService) Login(userName string, password string) (*adminModel.User, error) {
    if userName == "" {
        return nil, commonError.New(commonError.CodeMissingParameter, "用户名不能为空", nil)
    }
    if password == "" {
        return nil, commonError.New(commonError.CodeMissingParameter, "密码不能为空", nil)
    }
	userInfo, err := s.UserRepo.GetByUserName(userName)
    if err.Code == commonError.CodeRecordNotFound {
        return nil, commonError.New(commonError.CodeUserNotFound, "用户不存在", nil)
    }
	if err != nil {
		return nil, err
	}

	if common.CheckPassword(userInfo.Password, password) {
		return userInfo, nil
    } else {
        return nil, commonError.New(commonError.CodeInvalidCredentials, "用户名或密码错误", nil)
    }
}
